﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using AndBurn.DDSReader;

using DnPakExplorer.Forms;

namespace DnPakExplorer.Controls {
	public partial class DragonJewelSlotsCtrl : UserControl {
		BagForm mBagForm;
		IUserItem mItem;
		List<DragonJewelSlotBox> mSlots = new List<DragonJewelSlotBox>();

		public DragonJewelSlotsCtrl() {
			InitializeComponent();
			this.Disposed += DragonJewelSlotsCtrl_Disposed;
		}

		void DragonJewelSlotsCtrl_Disposed(object sender, EventArgs e) {
			Cleanup();
		}

		void Cleanup() {
			if (mItem != null) {
				foreach (DragonJewelSlot dj in mItem.AppliedSlotsList) {
					dj.OnItemChanged -= OnItemChanged;
				}
			}
			mSlots.Clear();
		}

		private void DragonJewelSlotsCtrl_Paint(object sender, PaintEventArgs e) {
			foreach (DragonJewelSlotBox slot in mSlots) {
				Point offset = slot.Position;
				e.Graphics.TranslateTransform(offset.X, offset.Y);
				slot.OnDraw(e.Graphics);
				e.Graphics.TranslateTransform(-offset.X, -offset.Y);
				//g.DrawRectangle(Pens.White, slot.Bounds);
			}
		}

		

		public void SetItem(IUserItem item) {
			Cleanup();
			mItem = item;
			Point startPoint = new Point(0, 0);
			PartImage backImage = new PartImage();
			backImage = new PartImage();
			backImage.ImageSource = new DDSImage((byte[])Properties.Resources.uit_item_slot04).BitmapImage;
			backImage.SourceRect = new Rectangle(2, 2, 50, 50);
			backImage.DestRect = new Rectangle(0, 0, 50, 50);
			foreach (DragonJewelSlot dj in item.AppliedSlotsList) {
				DragonJewelSlotBox slot = new DragonJewelSlotBox();
				slot.Bounds = new Rectangle(startPoint, new Size(47, 47));
				slot.TargetSlot = dj;
				slot.ItemDestRect = new Rectangle(-2, -2, 50, 50);
				slot.Background = backImage;
				slot.TypeImage = new PartImage();
				slot.TypeImage.ImageSource = Util.GetJewelTypeImage(dj.DragonJewelSlotType);
				slot.TypeImage.SourceRect = new Rectangle(0, 0, 50, 50);
				slot.TypeImage.DestRect = new Rectangle(2, 2, 50, 50);
				dj.OnItemChanged += OnItemChanged;
				mSlots.Add(slot);
				startPoint.X += slot.Bounds.Width;
			}
				
			
		}

		public void OnItemChanged(IItemSlot slot, IUserItem oldItem) {
			Invalidate();
		}

		private DragonJewelSlotBox FindSlot(Point point) {
			foreach (DragonJewelSlotBox slot in mSlots) {
				if (slot.Bounds.Contains(point))
					return slot;
			}
			return null;
		}

		private void DragonJewelSlotsCtrl_MouseDown(object sender, MouseEventArgs e) {
			DragonJewelSlotBox slot = FindSlot(e.Location);
			if (slot != null) {
				if (e.Button == System.Windows.Forms.MouseButtons.Left && slot.TargetSlot != null) {
					if (mBagForm == null || mBagForm.IsDisposed) {
						mBagForm = new BagForm((Character)DnPlayerUtil.CurrentCharacter);
					}
					mBagForm.SetUseTarget(new DragonItemUseTarget(slot.TargetSlot, this));
					if (!mBagForm.Visible)
						mBagForm.Show(this);
				} else if (e.Button == System.Windows.Forms.MouseButtons.Right) {
					if (slot.Item != null && DnPlayerUtil.CurrentCharacter!=null) {
						DnPlayerUtil.CurrentCharacter.BagPanel.AddItem(slot.TargetSlot.Unequip());
					}
				}
			}
		}

		private void DragonJewelSlotsCtrl_MouseMove(object sender, MouseEventArgs e) {
			DragonJewelSlotBox slot = FindSlot(e.Location);
			if (slot != null && slot.Item != null) {
				InfoOverlayForm.ShowItem(slot.Item);
			} else {
				InfoOverlayForm.HideItem();
			}
		}

		private void DragonJewelSlotsCtrl_MouseLeave(object sender, EventArgs e) {
			InfoOverlayForm.HideItem();
		}

		private class DragonItemUseTarget : IItemUseTarget {
			private DragonJewelSlot mSlot;
			private DragonJewelSlotsCtrl mCtrl;

			public DragonItemUseTarget(DragonJewelSlot slot, DragonJewelSlotsCtrl ctrl) {
				mSlot = slot;
				mCtrl = ctrl;
			}

			public void UseItem(BagPanel bag, IUserItem item) {
				if (item == null)
					return;
				if (mSlot.CanEquip(item)) {
					IUserItem[] oldItems = mSlot.Equip(item);
					bag.RemoveItem(item);
					if (oldItems != null) {
						foreach (IUserItem it in oldItems) {
							bag.AddItem(it);
						}
					}
					mCtrl.Invalidate();
				}
			}

		}

		
	}
}
